/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestor.base.de.datos;

import java.io.*;
import java.util.*;

/**
 *
 * @author Ana Silvia Gonzáles
 * @author Diego Enríquez
 */
public class Constraint implements Serializable{
    
    String id;
    ArrayList<Key> keys;
    ArrayList<Check> checks;
    
    
    public Constraint(String id)
    {
        
        this.id=id;
        this.keys=new ArrayList<>();
        this.checks=new ArrayList<>();
        
    }
    
    /**
     * Revisa que se cumplan las condiciones del check para el registro
     * @param tabla
     * @param registro
     * @return true si se cumple, si no fale
     */
    public boolean check(Table tabla, ArrayList<String> registro)
    {
        boolean resp=true;
        
        int i=0;
        /*while((resp)&&(i<keys.size()))
        {
            
            if(keys.get(i).getTipo()==1)
            {
                resp=false;
            }
            
            i++;
            
        }
        
        i=0;*/
        while((resp)&&(i<checks.size()))
        {
            if(!checks.get(i).checks(tabla, registro))
            {
                resp=false;
            }
            
            i++;
        }
        
        return resp;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setKeys(ArrayList<Key> keys) {
        this.keys = keys;
    }

    public void setChecks(ArrayList<Check> checks) {
        this.checks = checks;
    }

    public String getId() {
        return id;
    }

    public ArrayList<Key> getKeys() {
        return keys;
    }

    public ArrayList<Check> getChecks() {
        return checks;
    }
    
    public void addCheck(Check newCheck)
    {
        
        this.checks.add(newCheck);
        
    }
    
    public void addKey(Key key)
    {
        this.keys.add(key);
    }
    
}
